package com.central.apps.controller;

import cn.hutool.core.map.MapUtil;
import com.central.app.service.HandlerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/StorageRoom")
@Slf4j
public class StorageRoomController {
    @Autowired private HandlerService handlerService;
    @PostMapping("/getStorageRoom")
    public List<Map<String,Object>> getStorageRoom (@RequestBody Map<String,Object> params){
        List<Map<String, Object>> hashMap1 = new ArrayList<>();
        String storenum = MapUtils.getString(params, "storenum");
        List<Map<String, Object>> bySql2 = handlerService.findBySql("SELECT distinct rowdesc from gq_areashelf where storenum ='" + storenum + "'");
        if (CollectionUtils.isNotEmpty(bySql2)){
            for (int i=0;i<bySql2.size();i++){
                HashMap<String, Object> hashMap = new HashMap<>();
                Map<String, Object> map = bySql2.get(i);
                String rowdesc = MapUtils.getString(map, "rowdesc");
                hashMap.put("rowdesc",rowdesc);
                hashMap.put("storenum",storenum);
                List<Map<String, Object>> bySql = handlerService.findBySql("SELECT\n" +
                        "\t\tcount(0) as number\n" +
                        "\tFROM\n" +
                        "\t\t(\n" +
                        "\t\tSELECT\n" +
                        "\t\t\tdistinct shelfnum \n" +
                        "\t\tFROM\n" +
                        "\t\t\tgq_areashelf \n" +
                        "\t\tWHERE\n" +
                        "\t\tstorenum = '"+storenum+"' and rowdesc ='"+rowdesc+"'\n" +
                        "\t) TOTAL");
                if (MapUtil.getInt(bySql.get(0), "number")>0){

                        hashMap.put("shelfnum",bySql.get(0).get("number"));
                }


                List<Map<String, Object>> bySql3 = handlerService.findBySql("select shelfnum from gq_areashelf where rowdesc='" + rowdesc + "'and storenum='"+storenum+"'");
                //查的是货架下的货位
                if(CollectionUtils.isNotEmpty(bySql3)){
                    int itemnumber=0;
                    for (Map<String, Object> stringObjectMap : bySql3) {
                        String shelfnum = MapUtils.getString(stringObjectMap, "shelfnum");
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select count(0) as numbers from gq_invbalances where storenum = '" + storenum + "' and binnum = '" + shelfnum + "'");
                        if(CollectionUtils.isNotEmpty(bySql1)){
                            Map<String, Object> map1 = bySql1.get(0);
                            itemnumber= itemnumber+MapUtils.getInteger(map1,"numbers");
                        }
                    }
                    hashMap.put("itemnum",itemnumber);
                    hashMap1.add(hashMap);
                }
            }
        }
        return  hashMap1;
    }
    @PostMapping("/getRowdesc")
    public  List<Map<String,Object>> getRowdesc (@RequestBody Map<String,Object> params){
        List<Map<String, Object>> hashMap1 = new ArrayList<>();
        String storenum = MapUtils.getString(params, "storenum");
        String rowdesc = MapUtils.getString(params, "rowdesc");
        List<Map<String, Object>> bySql = handlerService.findBySql("select shelfnum,shelfdesc from gq_areashelf where storenum='" + storenum + "' and rowdesc='"+rowdesc+"'");
        if (CollectionUtils.isNotEmpty(bySql)){
            for (int i=0;i<bySql.size();i++){
                HashMap<String, Object> hashMap = new HashMap<>();
                Map<String, Object> map = bySql.get(i);
                String shelfnum = MapUtils.getString(map, "shelfnum");
                String shelfdesc = MapUtils.getString(map, "shelfdesc");
                hashMap.put("shelfdesc",shelfdesc);
                List<Map<String, Object>> bySql1 = handlerService.findBySql("SELECT\n" +
                        "\t\tcount(0) as numbers\n" +
                        "\tFROM\n" +
                        "\t\t(\n" +
                        "\t\tSELECT\n" +
                        "\t\t\t itemnum \n" +
                        "\t\tFROM\n" +
                        "\t\t\tgq_invbalances \n" +
                        "\t\tWHERE\n" +
                        "\t\tstorenum = '"+storenum+"' and  binnum ='"+shelfnum+"'\n" +
                        "\t) TOTAL");
                if (MapUtil.getInt(bySql1.get(0), "numbers")>0){
                    hashMap.put("itemnum",bySql1.get(0).get("numbers"));

                }else {
                    hashMap.put("itemnum",0);
                }
                hashMap1.add(hashMap);
            }

        }

        return hashMap1;
    }
}
